package com.higgins.lawapp.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.higgins.lawapp.client.settings.dataItemModel.DataItemContainerDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemInstanceDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemLevelDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemSourceDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceLevelDTO;
import com.higgins.lawapp.client.settings.rpc.DataItemService;
import com.higgins.lawapp.client.settings.rpc.DataSourceService;
import com.higgins.lawapp.server.dataItemModel.DataItem;
import com.higgins.lawapp.server.dataItemModel.DataItemContainer;
import com.higgins.lawapp.server.dataItemModel.DataItemInstance;
import com.higgins.lawapp.server.dataItemModel.DataItemLevel;
import com.higgins.lawapp.server.dataItemModel.DataItemSource;
import com.higgins.lawapp.server.dataModel.DataSource;
import com.higgins.lawapp.server.dataModel.DataSourceLevel;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions.Builder;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;


/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class DataItemServiceImpl extends RemoteServiceServlet implements DataItemService {
	DatastoreService myDatastoreService = DatastoreServiceFactory.getDatastoreService();
	DataCollectionEngine dataCollectionEngine = new DataCollectionEngine();
	DataCollectionStoreEngine dataCollectionStoreEngine = new DataCollectionStoreEngine();

	public String updateDataItemSource(DataItemSource dataItemSource)	{
		PersistenceManager pm=PMF.get().getPersistenceManager();
		String status = "Successfully Updated!!!";
		try	{
			dataCollectionStoreEngine.updateDataItemSource(dataItemSource);
		} catch (Exception e)	{
			status="Unsuccessfully updated: " + e.toString();
		}
		pm.close();
		return status;
	}
	
	public List<DataItemSourceDTO> retrieveDataItems() throws IllegalArgumentException	{
		PersistenceManager pm=PMF.get().getPersistenceManager();
		List<DataItemSourceDTO> dataItemSourceDTOList = new ArrayList<DataItemSourceDTO>();
		List<DataItemSource> dataItemSourceList;
		DataItemLevel dataItemLevel;

		Query query;
		query=pm.newQuery(DataItemSource.class);
		try	{
			dataItemSourceList = (List<DataItemSource>) query.execute();
			Iterator<DataItemSource> iterator = dataItemSourceList.iterator();
			
			while(iterator.hasNext())	{
				DataItemSource dataItemSource = iterator.next();
				DataItemSourceDTO dataItemSourceDTO = dataItemSource.getDataItemSourceDTO();
				dataItemSourceDTOList.add(dataItemSourceDTO);
			}
			
		} catch (Exception e)	{
			System.out.println(e.toString());
		}
		pm.close();

		return dataItemSourceDTOList;
	}
	
	public String deleteDataItem(DataItemDTO dataItemDTO) throws IllegalArgumentException	{
		PersistenceManager pm=PMF.get().getPersistenceManager();
		Query query;
		query=pm.newQuery(DataItem.class, "dataItemOne==paramDataItemOne && dataItemTwo==paramDataItemTwo");
		try {
			query.declareParameters("String paramDataItemOne, String paramDataItemTwo");
			List<DataItem> list = (List<DataItem>)query.execute(dataItemDTO.getItemOne(), dataItemDTO.getItemTwo());
			
			query.deletePersistentAll(dataItemDTO.getItemOne(), dataItemDTO.getItemTwo());
		} catch (Exception e)	{
			System.out.println("Something went wrong " + e.toString());
		}
		pm.close();

		return "Success";
	}

	public String deleteDataItemLevel(DataItemLevelDTO dataItemLevelDTO)
			throws IllegalArgumentException {
		PersistenceManager pm=PMF.get().getPersistenceManager();
		Query query;
		query=pm.newQuery(DataItemLevel.class, "dataItemLevelName==paramDataItemLevelName");
		try {
			query.declareParameters("String paramDataItemLevelName");
			List<DataItemLevel> list = (List<DataItemLevel>)query.execute(dataItemLevelDTO.getDataItemLevelName());
			query.deletePersistentAll(dataItemLevelDTO.getDataItemLevelName());
		} catch (Exception e)	{
			System.out.println("Something went wrong " + e.toString());
		}
		pm.close();

		return "Success";
	}

	public String deleteDataItemSource(DataItemSourceDTO dataItemSourceDTO)
			throws IllegalArgumentException {
		PersistenceManager pm=PMF.get().getPersistenceManager();
		Query query;
		query=pm.newQuery(DataItemSource.class, "dataSourceName==paramDataSourceName");
		try {
			query.declareParameters("String paramDataSourceName");
			List<DataItem> list = (List<DataItem>)query.execute(dataItemSourceDTO.getDataItemSourceName());
			query.deletePersistentAll(dataItemSourceDTO.getDataItemSourceName());
		} catch (Exception e)	{
			System.out.println("Something went wrong " + e.toString());
		}
		pm.close();

		return "Success";
	}
	
	public DataItemContainerDTO retrieveDataItemContainers(String dataSource, String dataLevel, String dataInstance) throws IllegalArgumentException	{
		List<DataItemContainer> dataItemContainers = new ArrayList<DataItemContainer>();
		DataItemContainer dataItemContainer = null;
		PersistenceManager pm=PMF.get().getPersistenceManager();
		Query query;
		query=pm.newQuery(DataItemContainer.class);
		try	{
			dataItemContainers = (List<DataItemContainer>) query.execute();		// ADD SEARCH CRITERIA
			if (dataItemContainers.size() > 0)
				dataItemContainer = dataItemContainers.get(0);
		} catch (Exception e)	{
			System.out.println("Error in retrieveDataItemContainers() " + e.toString());
		}
		DataItemContainerDTO dataItemContainerDTO = dataItemContainer.getDataItemContainerDTO();
		pm.close();
		return dataItemContainerDTO;
	}
	
}
